<?php
/**
 * 成员管理  逻辑层
 */

namespace App\Http\Repositories\UserCenter;


use App\Exceptions\ApiException;
use App\Http\Middleware\Authenticate;
use App\Http\Repositories\BaseRepository as Repository;
use App\Models\UserModels\Models\UserAccountModel;
use Tymon\JWTAuth\Facades\JWTAuth;

class UserRepository extends Repository
{
    public $account;
    public function __construct(UserAccountModel $account)
    {
        $this->account = $account;
    }

    /**
     * 获取用户详情
     * @param $data
     * @return array|mixed
     * @throws ApiException
     */
    public function getUserInfo($data)
    {
        $userInfo = null;

        if (isset($data['id'])) {
            $userInfo = $this->account->find($data['id']);
        } elseif (isset($data['uuid'])) {
            $where['member_uuid'] = $data['uuid'];
            $userInfo = $this->account->getFirst($where);
        }

        if (is_null($userInfo))
            throw new ApiException(ApiException::ERROR_DATA_NOT_EXISTS);

        return $userInfo;
    }

    /**
     * 添加用户
     * @param $data
     * @return mixed
     * @throws ApiException
     */
    public function addUser($data)
    {
        $where['member_name'] = $data['member_name'];
        if ($this->account->handleCondition($where)->first()) {
            throw new ApiException([0, '用户名已注册']);
        }

        if (is_real_exists(@$data['member_email'])) {
            if ($this->account->handleCondition(['member_email' => $data['member_email']])->first()) {
                throw new ApiException([0, '邮箱已注册']);
            }
        }

        if (is_real_exists(@$data['member_phone'])) {
            if ($this->account->handleCondition(['member_phone' => $data['member_phone']])->first()) {
                throw new ApiException([0, '号码已注册']);

            }
        }

        return $this->account->addItem($data);
    }

    /**
     * 用户登录
     * @param $data
     * @return array
     * @throws ApiException
     */
    public function loginMember($data)
    {
        $where['member_password'] = md5($data['member_password']);
        is_real_exists(@$data['member_name']) && $where['member_name'] = $data['member_name'];
        is_real_exists(@$data['member_phone']) && $where['member_phone'] = $data['member_phone'];
        is_real_exists(@$data['member_email']) && $where['member_email'] = $data['member_email'];
        $field = 'id,member_name,member_uuid,member_token';
        $userInfo = $this->account->getFirst($where, $field);

        if (is_null($userInfo)) {
            throw new ApiException(ApiException::ERROR_DATA_NOT_EXISTS);
        }

        \Cache::set(Authenticate::CACHE_KEY.$userInfo->member_token, json_encode($userInfo->toArray()));

        return $userInfo;
    }

    /**
     * @param $data
     * @return mixed
     */
    public function getUserList($data)
    {
        !isset($data['type']) && $data['type'] = 1;

        $where = [];
        is_real_exists(@$data['member_name']) && $where['member_name'] = ['like', '%'.$data['member_name'].'%'];
        is_real_exists(@$data['member_email']) && $where['member_email'] = ['like', '%'.$data['member_email'].'%'];
        is_real_exists(@$data['member_phone']) && $where['member_phone'] = ['like', '%'.$data['member_phone'].'%'];
        is_real_exists(@$data['status']) && $where['status'] = $data['status'];

        return $this->account->getUserList($data['type'], $where);
    }

    /**
     * 编辑用户
     * @param $data
     * @return array
     * @throws ApiException
     */
    public function editUser($data)
    {
        $user = $this->account->find($data['id']);
        if (is_null($user)) {
            throw new ApiException(ApiException::ERROR_DATA_NOT_EXISTS);
        }

        if (isset($data['userInfo'])) {
            $user->userInfo()->update($data['userInfo']);
            unset($data['userInfo']);
        }

        $user->update($data);

        return $user;
    }

    public function delUser($data)
    {
        $id = explode(',', $data['ids']);
        $success = 0;
        $fail = 0;
        $message = '';
        $update['is_delete'] = 1;
        foreach ($id as $k => $v) {
            $model = $this->account->find($v);
            if (empty($model)) {
                $fail ++;
                $message .= '第'.($k+1).'个删除失败的原因:数据不存在;';
            } else {
                $success ++;
                $model->update($update);
            }
        }

        return ['message' => '成功删除'.$success.'个;失败'.$fail.'个;'.$message];
    }
}
